/*
 * Created on Jun 8, 2004
 */
package org.vectrics.mail;

import java.util.Collection;
import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.vectrics.SystemException;
import org.vectrics.provider.CoreServiceLocator;


/**
 * @author mmoore
 */
public class NotificationManager extends NotificationProvider {
	private static final Log log = LogFactory.getLog(NotificationManager.class);
	private QueueConnection conn = null;
	private QueueSession session = null;
	private Queue jmsQueue = null;
	private String jndiName = "queue/notification";
	private boolean useJMS = false;
	/**
	 * 
	 */
	public NotificationManager() {
		super();
	}
	
	protected void doSendMessage(Collection persons, MailMessage message) throws MailException {
	    if (useJMS) {
	        doSendMessageJMS(persons, message);
	    } else {
	        doSendMessageNow(persons, message);
	    }
	    
	}
	
	protected void doSendMessageNow(Collection persons, MailMessage message) throws MailException {
		UserMailer mailer = new UserMailer(CoreServiceLocator.getConfiguration().getFromMailConfiguration(message.getDomain()));
		mailer.sendMessageAsynch(persons, message);
	}
	
	
	
	protected void doSendMessageJMS(Collection persons, MailMessage message) {
		Queue queue = this.getQueue();

		MailMessageOrder order = new MailMessageOrder();
		order.setPersons(persons);
		order.setMailMessage(message);

		try {
			session = this.conn.createQueueSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
			ObjectMessage objMessage = session.createObjectMessage();
			
			objMessage.setObject(order);
	
			QueueSender sender = session.createSender(queue);
			sender.send(objMessage);
			
			sender.close();
		} catch (JMSException je) {
			log.error("Exception sending out norification: " + message.getSubject(), je);
			throw new SystemException(
					"Exception sending out notification: " + message.getSubject(), je);
		}
	}

	
	public Queue getQueue() {
		if (jmsQueue == null) {
			try {
				InitialContext iniCtx = new InitialContext();
				Object tmp = iniCtx.lookup("ConnectionFactory");
				QueueConnectionFactory qFactory = (QueueConnectionFactory) tmp;
				conn = qFactory.createQueueConnection();
				jmsQueue = (Queue) iniCtx.lookup(jndiName);
			} catch (JMSException je) {
				throw new SystemException("JMSException in getQueue, jndi name = " + jndiName);
			} catch (NamingException je) {
				throw new SystemException("NamingException in getQueue, jndi name = " + jndiName);
			}
		}
		return(jmsQueue);
	}

	
	public void stop() throws JMSException	{
			conn.stop();
			session.close();
			conn.close();
	}

}
